package com.cn.onetrip.untility;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.StringBody;
//import org.apache.http.entity.mime.MultipartEntity;
//import org.apache.http.entity.mime.content.FileBody;
//import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;

import android.os.Handler;
import android.os.Message;

public class upload {

	HttpClient client = null;
	Thread thread = null;
	String httpUrl = "http://m.weather.com.cn/data/101270101.html";

	private int REQUEST_TIMEOUT = 10 * 1000;
	private int SO_TIMEOUT = 20 * 1000;

	String testCity = "";
	String reponseDataString = "";
	// SysApplication sysApp = new SysApplication();

	private Handler handler;

	public upload(Handler handler) {
		this.handler = handler;
	}

	// scenic content data
	public void Http_Get_Content(String urlPath) {
		InputStream content = null;
		if (client != null) {
			client = null;
		}
		client = this.getHttpClient();
		HttpGet httpGet = new HttpGet(urlPath);
		HttpResponse response = null;
		try {
			response = client.execute(httpGet);
			int code = response.getStatusLine().getStatusCode();
			if (code == HttpStatus.SC_OK) {
				HttpEntity entity = response.getEntity();
				content = entity.getContent();
				reponseDataString = convertStreamToString(content);

				Message msg = new Message();
				msg.what = 0;
				msg.obj = reponseDataString;
				handler.sendMessage(msg);
			} else {
				Message msg = new Message();
				msg.what = 1;
				msg.obj = reponseDataString;
				handler.sendMessage(msg);
			}
		} catch (IllegalStateException e) {
			System.out.println("ccccccccc");
			e.printStackTrace();
		} catch (IOException e) {
			System.out.println("ddddddddd");
			Message msg = new Message();
			msg.what = 1;
			msg.obj = reponseDataString;
			handler.sendMessage(msg);
			e.printStackTrace();
		}
	}

	private String convertStreamToString(InputStream is) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();
		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line);
			}
			String s = sb.toString();
		} catch (IOException e) {
			e.printStackTrace();

		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return sb.toString();
	}

	class ATask implements Runnable {

		public void run() {
			try {
				Thread.sleep(500);
				Message msg = new Message();
				msg.what = 2;
				handler.sendMessage(msg);
			} catch (InterruptedException e) {
				System.out.println("Exiting by Exception");
			}
		}
	}

	public HttpClient getHttpClient() {
		BasicHttpParams httpParams = new BasicHttpParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, REQUEST_TIMEOUT);
		HttpConnectionParams.setSoTimeout(httpParams, SO_TIMEOUT);
		HttpClient client = new DefaultHttpClient(httpParams);
		return client;
	}
}
